#define DEBUG
#include <cstdio>
#include <cstring>

using namespace std;

const int maxn=1000, maxm=500000;

int min(int a, int b) {
  return a<b ? a : b;
}

class Network {
public:
  int n, m=1, ind[maxn+1], link[2*maxm+2], to[2*maxm+2], val[2*maxm+2], dis[maxn+1], cur[maxn+1];

  void addSide(int u, int v, int w) {
    m++;
    to[m] = v;
    val[m] = w;
    link[m] = ind[u];
    ind[u] = m;
  }

  void calcDis(int s) {
    static int que[maxn+1];
    memset(dis, 255, sizeof dis);
    que[1] = s;
    dis[s] = 0;
    for (int h=0, t=1; h<t; ) {
      h++;
      for (int i=ind[que[h]]; i; i=link[i]) {
	if (val[i] && dis[to[i]]<0) {
	  dis[to[i]] = dis[que[h]]+1;
	  que[++t] = to[i];
	}
      }
    }
  }

  void resetCur() {
    for (int i=1; i<=n; i++) {
      cur[i] = ind[i];
    }
  }

  int getFlow(int o, int t, int flow) {
    if (o==t) {
      return flow;
    }
    int have=0, run;
    for (int i=cur[o]; i && have<flow; i=link[i]) {
      if (dis[o]+1==dis[to[i]]) {
	run = getFlow(to[i], t, min(val[i], flow-have));
	val[i] -= run, val[i^1] += run, have += run;
      }
      cur[o] = i;
    }
    return have;
  }

  int solve(int s, int t) {
    const int inf=0x7fffffff;
    int ret=0;
    for (calcDis(s); dis[t]>=0; calcDis(s)) {
      resetCur();
      ret += getFlow(s, t, inf);
    }
    return ret;
  }
};

int main() {
#ifdef DEBUG
  freopen("src.in", "r", stdin);
  freopen("src.out", "w", stdout);
#endif

  static Network net;
  int n, m;
  scanf("%d %d", &m, &n);
  net.n = n;
  for (int i=1; i<=m; i++) {
    int x, y, z;
    scanf("%d %d %d", &x, &y, &z);
    net.addSide(x, y, z);
    net.addSide(y, x, 0);
  }

  printf("%d", net.solve(1, n));

  return 0;
}
